<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends REST_Controller {

    function __construct(){  
        
        parent::__construct();  
        $this->returnInfo = lang('returnInfo');
        $this->rdata['returnNo'] = '0000';
        $this->rdata['returnInfo'] = $this->returnInfo['0000'];
        $this->rdata['returnTime'] = time();
        $this->rdata['returnData'] = (object)array();
    }

    public function index_get() {

        echo 'welcome.';

        // $this->load->model('test_model', 'mtest');
        // // $row = $this->mtest->update_one_cache(FALSE,array('modify_at' => date('Y-m-d H:i:s')),array('id >' => 0));
        // $row = $this->mtest->get_list_where_cache(array('id >' => 0));
        // var_dump($row);
    }

    public function list_get(){


        //新闻类型 默认1
        //字典 行业新闻 1 发标公告 2 网站公告 3 活动公告 4  特别的 type=-1是指所有的新闻的前10条(首页推荐用)
        $type = intval($this->get('type'));
        if (!$type || !is_numeric($type)) {

            $type = 1;
        } 

        //页数 默认1
        $page = intval($this->get('page'));
        if (!$page || !is_numeric($page)) {

            $page = 1;
        }    

        //每页尺寸 默认1
        $size = intval($this->get('size'));
        if(!$size || !is_numeric($size)){

            $size = 10;
        }

        $where = array();
        switch ($type) {
            case -1:
                #全部 不用加where
                $where = array('in'=>array('ctype',array(2,3,4)));
                break;
            case -2:
                $where = array('in'=>array('ctype',array(2,3,4)));
                break;
            default:
                $where = array('ctype'=>$type);
                break;
        }

        $where['cid'] = 48;

        $where['status'] = 1;
        $this->load->model('article_model','marticle','default');

        $limit  = $size;
        $start  = ($page-1)*$size;
        $order  = array("id"=>'desc');
        $fields = array();
        $fields[] = "id";
        $fields[] = "title";
        $fields[] = "ctype as type";
        $fields[] = "DATE_FORMAT(create_at,'%Y-%m-%d') as create_at";
        $fields[] = "photo";
        $table  = FALSE;
        $expire = 604800; //stroing for a week;
        $total  = $this->marticle->get_count_all_cache($where,FALSE,$expire);
        $result = $this->marticle->get_list_where_cache($limit,$start,$order,$where,$fields,FALSE,$expire);
        //数据库错误
        if (!$result) {
            $this->response($this->rdata);
        }

        foreach($result as &$value){
            if ($value['photo']) {
                $photo = one_upload($value['photo']);
                $value['photo'] = UPLOAD_URL.$photo['thumb'];
            }
            
        }

        $this->rdata['returnData']->list = $result;
        $this->rdata['returnData']->total = (int)$total;
        $this->rdata['returnData']->page = $page;
        $this->response($this->rdata);
    }


    public function info_get(){

        $id = intval($this->get('id'));
        if (!$id || !is_numeric($id)) {

            $this->rdata['returnNo'] = '0011';
            $this->rdata['returnInfo'] = $this->returnInfo['0011'];
            $this->response($this->rdata);
        }

        $this->load->model("article_model","marticle",'default');

        $fields = array();
        $fields[] = "id";
        $fields[] = "cid";
        $fields[] = "title";
        $fields[] = "content";
        $fields[] = "sort_id";
        $fields[] = "ctype";
        $fields[] = "photo";
        $fields[] = "DATE_FORMAT(create_at,'%Y-%m-%d') as create_at";
        $result = $this->marticle->get_one_pn_cache($id,$fields,FALSE,604800);

         //数据库错误
        if (!$result) {
            $this->response($this->rdata);
        }

        $this->rdata['returnData']->id = $result['id'];
        $this->rdata['returnData']->title = $result['title'];
        $this->rdata['returnData']->content = $result['content'];
        $this->rdata['returnData']->create_at = $result['create_at'];
        $this->rdata['returnData']->type = $result['ctype'];
        $this->rdata['returnData']->photo = "";
        if ($result['photo']) {
            $photo = one_upload($result['photo']);
            $this->rdata['returnData']->photo = UPLOAD_URL.$photo['thumb'];
        }
        

        if (isset($result['prev_id'])) {
            $this->rdata['returnData']->prev_id = $result['prev_id'];
            $this->rdata['returnData']->prev_title = $result['prev_title'];
        }

        if (isset($result['next_id'])) {
            $this->rdata['returnData']->next_id = $result['next_id'];
            $this->rdata['returnData']->next_title = $result['next_title'];
        }
        $this->response($this->rdata);
    }

    public function die_post(){

        $this->load->driver('cache', array('adapter' => 'redis'));
        $keys = $this->cache->get_keys('*article*');

        foreach ($keys as $value) {
            $this->cache->delete($value);
        }
    }
}